package com.elitetyc.eliteim.server.handler;

import com.elitetyc.eliteim.common.proto.RequestProto;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
import io.netty.handler.timeout.IdleStateHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author yuanchao.tan
 * @version 1.0
 * @description TODO
 * @Date 2021/9/16 10:30
 */
@Component
public class ServeChannelInitializer extends ChannelInitializer<Channel> {
    @Autowired
    ServerChannelHandler serverChannelHandler;
    @Override
    protected void initChannel(Channel ch) throws Exception {
        ch.pipeline()
                // 服务端20秒未接收消息后触发，当触发后，会回调ChannelHandler#userEventTriggered()方法
                .addLast(new IdleStateHandler(20,0,0))
                .addLast(new ProtobufVarint32FrameDecoder())
                .addLast(new ProtobufDecoder(RequestProto.RequestProtocol.getDefaultInstance()))
                .addLast(new ProtobufVarint32LengthFieldPrepender())
                .addLast(new ProtobufEncoder())
                .addLast(serverChannelHandler);
    }
}
